package com.ourcraft.informationcollectionsystem.service.common.addQueryHistory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.CompanyInfoHistory;
import com.ourcraft.informationcollectionsystem.entity.DataReportFormHistory;
import com.ourcraft.informationcollectionsystem.mapper.CompanyInfoHistoryMapper;
import com.ourcraft.informationcollectionsystem.mapper.DataReportFormHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * QueryHistoryServiceImpl provide history-query-related services interface implementation.
 *
 * @author Liu Yiming
 * @since 2023-11-04
 */
@Service
public class QueryHistoryServiceImpl implements QueryHistoryService {
    @Autowired
    CompanyInfoHistoryMapper companyInfoHistoryMapper;
    @Autowired
    DataReportFormHistoryMapper dataReportFormHistoryMapper;

    /**
     * Retrieves all examine histories of a report out by company ID.
     *
     * @param companyId The ID of the company.
     * @return all examine histories of the report.
     */
    @Override
    public ArrayList<Map<String, Object>> getCompanyInfoHistory(int companyId) {
        // 筛选表单
        QueryWrapper<CompanyInfoHistory> companyHistoryQueryWrapper = new QueryWrapper<>();
        companyHistoryQueryWrapper.eq("company_id", companyId);
        List<CompanyInfoHistory> companyInfoHistoryList = companyInfoHistoryMapper.selectList(companyHistoryQueryWrapper);
        ArrayList<Map<String, Object>> returnData = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (CompanyInfoHistory history : companyInfoHistoryList) {
            Map<String, Object> map = new HashMap<>();
            // 添加键值对
            map.put("historyType", history.getHistoryType());
            map.put("historyComment", history.getHistoryComment());
            Date historyDate = history.getHistoryDate();
            // 格式化日期
            String dateStr = dateFormat.format(historyDate);
            map.put("historyDate", dateStr);
            returnData.add(map);
        }
        return returnData;
    }

    /**
     * Retrieves all examine histories of a form out by form ID.
     *
     * @param formId The ID of the form.
     * @return all examine histories of the form.
     */
    @Override
    public ArrayList<Map<String, Object>> getDataReportFormHistory(int formId) {
        // 筛选表单
        QueryWrapper<DataReportFormHistory> formHistoryQueryWrapper = new QueryWrapper<>();
        formHistoryQueryWrapper.eq("form_id", formId);
        List<DataReportFormHistory> dataReportFormHistoryList = dataReportFormHistoryMapper.selectList(formHistoryQueryWrapper);
        ArrayList<Map<String, Object>> returnData = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (DataReportFormHistory history : dataReportFormHistoryList) {
            Map<String, Object> map = new HashMap<>();
            // 添加键值对
            map.put("historyType", history.getHistoryType());
            map.put("historyComment", history.getHistoryComment());
            Date historyDate = history.getHistoryDate();
            // 格式化日期
            String dateStr = dateFormat.format(historyDate);
            map.put("historyDate", dateStr);
            returnData.add(map);
        }
        return returnData;
    }
}
